package com.three.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class HttpUtil {

    private static final String BASIC_URL = "http://10.96.36.47:573";
    private static final CloseableHttpClient client = HttpClientBuilder.create().build();
    private static CloseableHttpResponse response;

    private HttpUtil() {
    }
    public static JSONObject get(String url, HashMap<String, String> data) throws URISyntaxException, IOException {
        HttpGet httpGet;
        if (data != null) {
            URIBuilder uriBuilder = new URIBuilder(BASIC_URL + url);
            for (String key : data.keySet()) {
                uriBuilder.addParameter(key, data.get(key));
            }
            httpGet = new HttpGet(uriBuilder.build());
        } else {
            httpGet = new HttpGet(BASIC_URL + url);
        }
        String json = EntityUtils.toString(client.execute(httpGet).getEntity(), StandardCharsets.UTF_8);
        return JSON.parseObject(json);
    }

    public static JSONObject post(String url, HashMap<String, String> param) throws IOException {
        HttpPost httpPost = new HttpPost(BASIC_URL + url);
        if (param != null) {
            List<BasicNameValuePair> data = new ArrayList<>();
            for (String key : param.keySet()) {
                data.add(new BasicNameValuePair(key, param.get(key)));
            }
            UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(data, StandardCharsets.UTF_8);
            httpPost.setEntity(urlEncodedFormEntity);
        }
        String json = EntityUtils.toString(client.execute(httpPost).getEntity(), StandardCharsets.UTF_8);
        return JSON.parseObject(json);
    }

    public static JSONObject post(String url, String fileName, File file) throws IOException {
        HttpPost httpPost = new HttpPost(BASIC_URL + url);
        // 构建多部分实体
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addBinaryBody("avatar", new File("1.png"), ContentType.IMAGE_JPEG, "image.jpg");
        HttpEntity httpEntity = builder.build();
        // 设置请求实体
        httpPost.setEntity(httpEntity);
        String json = EntityUtils.toString(client.execute(httpPost).getEntity(), StandardCharsets.UTF_8);
        return JSON.parseObject(json);
    }
}
